package com.marion.ddd.service.impl;

import com.marion.ddd.entity.AccountEntity;
import com.marion.ddd.repository.AccountRepository;
import com.marion.ddd.service.TAccountService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 用户账户表(TAccount)表服务实现类
 *
 * @author makejava
 * @since 2021-12-04 11:35:40
 */
@Service("TAccountService")
public class TAccountServiceImpl implements TAccountService {
    @Resource
    private AccountRepository tAccountDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AccountEntity queryById(Integer id) {
        return this.tAccountDao.findById(id).orElse(null);
    }

    /**
     * 分页查询
     *
     * @param accountEntity 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    @Transactional
    public Page<AccountEntity> queryByPage(AccountEntity accountEntity, Pageable pageable) {
        Example<AccountEntity> example = Example.of(accountEntity);
        long total = this.tAccountDao.count(example);
        return new PageImpl<>(this.tAccountDao.findAll(example, pageable).getContent());
    }

    /**
     * 新增数据
     *
     * @param accountEntity 实例对象
     * @return 实例对象
     */
    @Override
    public AccountEntity insert(AccountEntity accountEntity) {
        this.tAccountDao.save(accountEntity);
        return accountEntity;
    }

    /**
     * 修改数据
     *
     * @param accountEntity 实例对象
     * @return 实例对象
     */
    @Override
    public AccountEntity update(AccountEntity accountEntity) {
        this.tAccountDao.save(accountEntity);
        return this.queryById(accountEntity.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        this.tAccountDao.deleteById(id);
        return true;
    }
}
